//
//  TankSprite.m
//  lust
//
//  Created by fan on 13-4-2.
//
//

#import "TankSprite.h"
#import "TankLayer.h"
@implementation TankSprite
@synthesize kAct;
@synthesize bIsEnemy;

+(id)TankWithinLayer:(CCLayer *)layer imageFile:(NSString *)imgFile
{
    TankLayer* ly=(TankLayer*)layer;
    CCSpriteBatchNode* animationSheet=[CCSpriteBatchNode batchNodeWithFile:imgFile capacity:3];
    [ly.gameWorld addChild:animationSheet z:99 tag:1];
    TankSprite* sprite=[TankSprite spriteWithTexture:animationSheet.texture rect:CGRectMake(0, 0, 40, 40)];
    [animationSheet addChild:sprite z:0 tag:1];
    [sprite setLayer:layer];
    [sprite TankInit];
    return sprite;
}

-(void)Activate
{
    NSArray* arr=[NSArray arrayWithObjects:[[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"e01"] , [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"e02"],nil];
    CCAnimation* animation=[CCAnimation animationWithSpriteFrames:arr delay:0.2];
    id action=[CCAnimate actionWithAnimation:animation];
    [self runAction:[CCRepeatForever actionWithAction:action]];
    [self schedule:@selector(DoRandomAction)interval:3];
}


-(void)Kill
{
    [self unschedule:@selector(KeepPlay)];
    [self unschedule:@selector(DoRandomAction)];
}

-(void)DoRandomAction
{
    float rand=CCRANDOM_0_1();
    if(rand<0.20)
    {
        kAct=kUp;
    }else if(rand<0.50)
    {
        kAct=kLeft;
    }else if(rand<0.80)
    {
        kAct=kRight;
    }else
    {
        kAct=kDown;
    }
    nRandomActionCount=0;
    [self schedule:@selector(KeepPlay)interval:1/30];
}
-(void)KeepPlay
{
    switch (kAct) {
        case kUp:
            [self MoveUp:gLayer];
        break;
        case kDown:
            [self MoveDown:gLayer];
        break;
        case kLeft:
            [self MoveLeft:gLayer];
        case kRight:
            [self MoveRight:gLayer];
        break;
    }
    nRandomActionCount=nRandomActionCount+1;
    if(nRandomActionCount>=75)
    {
        [self unschedule:@selector(KeepPlay)];
        [self OnFire:gLayer];
    }
}
-(void)setLayer:(CCLayer *)layer
{
    gLayer=layer;
}

-(void)TankInit
{
    turnSpeed=10;
    moveStep=2;
    kAct=kStay;
    
}
-(void)MoveUp:(CCLayer *)layer{}
-(void)MoveDown:(CCLayer *)layer
{}
-(void)MoveLeft:(CCLayer *)layer
{
    TankLayer* gl=(TankLayer*)layer;
    self.rotation=-90;
    unsigned int tid;
    CGPoint npt;
    npt=ccp(self.position.x- self.textureRect.size.width/2-moveStep,self.position.y);
    tid=[gl TileIdFromPosition:npt];
    if (tid!=4) {
        return;
    }
    
    npt=ccp(self.position.x- self.textureRect.size.width/2-moveStep,self.position.y+self.textureRect.size.height/4);
    tid=[gl TileIdFromPosition:npt];
    if (tid!=4) {
        return;
    }
    
    npt=ccp(self.position.x- self.textureRect.size.width/2-moveStep,self.position.y-self.textureRect.size.height/4);
    tid=[gl TileIdFromPosition:npt];
    if (tid!=4) {
        return;
    }
    
    self.position=ccp(self.position.x-moveStep, self.position.y);
    
}

-(void)MoveRight:(CCLayer *)layer{}
-(void)OnFire:(CCLayer *)layer{}
@end
